<!---
Provide functionality required to utilise oEmbed providers. 
see http://oembed.com for more details


Required Resources: 
- json.cfc -- http://www.epiphantastic.com/cfjson/

Authors: dannan (dannan.test@gmail.com)

Version: 1.0 January 2nd, 2010
--->
<cfcomponent displayname="oEmbed" output="false" extends="json">
	<!--- register commonly used oEmbed providers --->
	<cffunction name="init" access="public" returntype="void" output="false">
		<cfscript>
			addProvider("YouTube","http://*.youtube.com/*","http://www.youtube.com/oembed");
			addProvider("Flickr","http://*.flickr.com/*","http://www.flickr.com/services/oembed/");
			addProvider("Viddler","http://*.viddler.com/*","http://lab.viddler.com/services/oembed/");
			addProvider("Qik","http://qik.com/*","http://qik.com/api/oembed.json");
			addProvider("Qik","http://qik.com/video/*","http://qik.com/api/oembed.json");
			addProvider("Revision3","http://*.revision3.com/*","http://revision3.com/api/oembed/");
			addProvider("Hulu","http://www.hulu.com/watch/*","http://www.hulu.com/api/oembed.json");
			addProvider("Vimeo","http://www.vimeo.com/*","http://www.vimeo.com/api/oembed.json");
			addProvider("Poll Everywhere","http://www.polleverywhere.com/free_text_polls/*","http://www.polleverywhere.com/services/oembed/");
			addProvider("Poll Everywhere","http://www.polleverywhere.com/multiple_choice_polls/*","http://www.polleverywhere.com/services/oembed/");
			addProvider("Poll Everywhere","http://www.polleverywhere.com/polls/*","http://www.polleverywhere.com/services/oembed/");
			addProvider("Clearspring Widgets","http://www.clearspring.com/*","http://widgets.clearspring.com/widget/v1/oembed/");
		</cfscript>
	</cffunction>
	
	<!--- register a new provider --->
	<cffunction name="addProvider" access="public" returntype="void" output="false">
		<cfargument name="ProviderName" type="String" required="false" default="UnknownProvider" />
		<cfargument name="ProviderUrlScheme" type="String" required="true" />
		<cfargument name="ProviderApiEndPoint" type="string" required="true" />
		<cfset var i = 0 />
		
		<cfif Not IsDefined("application.aoEmbed")>
			<cfscript>
				application.aoEmbed = ArrayNew(2);
			</cfscript>
		</cfif>
		
		<cfscript>
			i = ArrayLen(application.aoEmbed)+1;
			application.aoEmbed[i][1] = arguments["ProviderName"];
			application.aoEmbed[i][2] = Replace(arguments["ProviderUrlScheme"],"*","(.*?)","ALL");
			application.aoEmbed[i][3] = arguments["ProviderApiEndPoint"];
		</cfscript>
		
	</cffunction>
	
	<cffunction name="doHttp" access="package" returntype="struct" output="false">
		<cfargument name="sURL" type="string" required="true" />
		<cfargument name="timeout" type="numeric" required="true" default="200" />
		
		<cfhttp method="get" url="#arguments["sURL"]#" useragent="oEmbed.cfc http parser" timeout="#arguments["timeout"]#"></cfhttp>
		
		<cfreturn cfhttp />
	</cffunction>
	
	<!--- embed a movie/picture/object and show it immidiately --->
	<cffunction name="embedhttp" output="false" returntype="string" access="public">
		<cfargument name="sURL" type="string" required="true" hint="Please provide the URL you wish to embed" />
		<cfreturn this.embed(arguments["sURL"]).html />
	</cffunction>
	
	<!--- embed a movie/picture/object and return a structure --->
	<cffunction name="embed" output="false" returntype="struct" access="public">
		<cfargument name="sURL" type="string" required="true" hint="Please provide the URL you wish to embed" />
		<cfset var a = 0 />
		<cfset var sEmbedUrl = "" />
		<cfset var stRet = StructNew() />
		<!--- make sure we have been instantiated --->
		<cfif Not IsDefined("application.aoEmbed")>
			<cfset this.init() />
		</cfif>
		<!--- see if we support this provider --->
		<cfloop from="1" to="#ArrayLen(application.aoEmbed)#" index="a">
			<cfset isTrue = REFind(application["aoEmbed"][a][2],arguments["sURL"],1,false) />
			<cfif isTrue>
				<cfset sEmbedUrl = application["aoEmbed"][a][3] & "?url=" & arguments["sURL"] />
				<cfif Not Find("json",application["aoEmbed"][a][3])>
					<cfset sEmbedUrl = sEmbedUrl & "&format=json" />
				</cfif>
				<cfbreak>
			</cfif>
		</cfloop>
		
		<cfif Len(Trim(sEmbedUrl))>
			<cfset stRet = this.decode(this.doHttp(sEmbedUrl).fileContent) />
			<cfreturn stRet />
		<cfelse>
			<cfset stRet = StructNew() />
			<cfset stRet.html = "<strong>Something went wrong embedding your URL</strong>" />
		</cfif>
	</cffunction>
</cfcomponent>